﻿using System.Collections.Generic;
using System.Threading.Tasks;
using ThresholdAssistant.Autofac;
using ThresholdAssistant.WebAPI.Models;

namespace ThresholdAssistant.WebAPI.Repository
{

    /// <summary>
    /// 系统参数表仓储
    /// </summary>
    public class ConfigRepository : BaseRepository, IBaseAutofac
    {
        /// <summary>
        /// 根据组ID查询列表
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<Config>> GetListByGroupIdAsync(long groupId)
        {
            var sql = @"
                SELECT t.id, t.group_id, t.config_name, t.config_value, t.config_type, t.create_time
	                , t.update_time, t.remark, t.control_type, t.control_icon
                FROM t_config t
                WHERE group_id = @groupId
            ";

            return await mydb.GetListAsync<Config>(sql, new { groupId });
        }


        /// <summary>
        /// 根据组ID查询列表
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<Config>> GetListByConfigTypeAsync(string configType)
        {
            var sql = @"
                SELECT t.id, t.group_id, t.config_name, t.config_value, t.config_type, t.create_time
	                , t.update_time, t.remark, t.control_type, t.control_icon
                FROM t_config t
                WHERE config_type = @ConfigType
            ";

            return await mydb.GetListAsync<Config>(sql, new { ConfigType = configType });
        }

        /// <summary>
        /// 新增
        /// </summary>
        public async Task<bool> AddAsync(Config model)
        {
            string insertSql = @"insert into `t_config`
(`group_id`,`config_name`,`config_value`,`config_type`,`create_time`,`update_time`,`remark`, control_type, control_icon)
values
(@GroupId,@ConfigName,@ConfigValue,@ConfigType,@CreateTime,@UpdateTime,@Remark, @ControlType, @ControlIcon)";
            return await mydb.ExecuteAsync(insertSql, model);
        }

        /// <summary>
        /// 新增列表
        /// </summary>
        public async Task<bool> AddListAsync(List<Config> list)
        {
            string insertSql = @"insert into `t_config`
(`group_id`,`config_name`,`config_value`,`config_type`,`create_time`,`update_time`,`remark`, control_type, control_icon)
values
(@GroupId,@ConfigName,@ConfigValue,@ConfigType,@CreateTime,@UpdateTime,@Remark, @ControlType, @ControlIcon)";
            return await mydb.ExecuteAsync(insertSql, list);
        }

        /// <summary>
        /// 查询
        /// </summary>
        public async Task<Config> GetInfoAsync(long id)
        {
            string querySql = @"select
 `id`, `group_id`, `config_name`, `config_value`, `config_type`, `create_time`, `update_time`, `remark`, control_type, control_icon
 from `t_config`
 where id = @Id;";
            return await mydb.GetFirstOrDefaultAsync<Config>(querySql, new { Id = id });
        }

        /// <summary>
        /// 修改
        /// </summary>
        public async Task<bool> UpdateAsync(Config model)
        {
            string updateSql = @"update
`t_config`
set  `group_id`=@GroupId, `config_name`=@ConfigName, `config_value`=@ConfigValue, `config_type`=@ConfigType, `create_time`=@CreateTime, `update_time`=@UpdateTime, `remark`=@Remark, control_type=@ControlType, control_icon=@ControlIcon
 where `id` = @Id";
            return await mydb.ExecuteAsync(updateSql, model);
        }

        /// <summary>
        /// 修改
        /// </summary>
        public async Task<bool> UpdateListAsync(List<Config> list)
        {
            string updateSql = @"update
`t_config`
set   `config_value`=@ConfigValue,  `update_time`=@UpdateTime
 where `id` = @Id";
            return await mydb.ExecuteAsync(updateSql, list);
        }

        /// <summary>
        /// 删除
        /// </summary>
        public async Task<bool> DeleteByGroupIdAsync(long groupId)
        {
            string updateSql = @"
                DELETE FROM `t_config` WHERE `group_id` = @groupId
            ";
            return await mydb.ExecuteAsync(updateSql, new { groupId });
        }
    }
}
